package com.example.seakdreamandroid.Lullaby;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import static com.example.seakdreamandroid.Constant.BASE_URL;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.seakdreamandroid.R;
import com.example.seakdreamandroid.crosstalk.Audio;
import com.example.seakdreamandroid.crosstalk.GridViewMethod;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class SelectedLullabyFragment extends Fragment {

    private ListView spShow;
    List<Map<String,Object>> list233 = new ArrayList<Map<String,Object>>();
    private int typelullaby;

    public SelectedLullabyFragment(int i) {
        typelullaby = i;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_selected_lullaby,container,false);
        spShow = (ListView) view.findViewById(R.id.fragment_selected_lullaby_listview);
        getDate();


        return view;
    }

    private void getDate() {
        Bundle bundle = new Bundle();
        bundle = this.getArguments();
        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
        String xtitle = bundle.getString("xxk");
        String type;
        switch (xtitle){
            case "摇篮曲":
                type = "lullaby";
                break;
            case "ASMR":
                type = "ASMR";
                break;
            case "戏曲":
                type = "opera";
                break;
            case "冥想学习":
                type = "learning";
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + xtitle);
        }
        String cate;
        switch (typelullaby){
            case 1:
                cate = "first";
                break;
            case 2:
                cate = "second";
                break;
            case 3:
                cate = "third";
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + xtitle);
        }

        System.out.println(type + "无此类" + cate);



        //2.创建请求对象（Request）,不需要设置请求体
        Request request = new Request.Builder()
                //设置请求的地址，默认的请求方式是Get请求
                .url(BASE_URL+"AudioInfo/findAudioInfo" + "?type="+type+"&category="+cate)
                //创建Request对象
                .build();
        //3.发送请求，获取响应
        OkHttpClient okHttpClient = new OkHttpClient();
        Call call = okHttpClient.newCall(request);
        //发起异步的请求，不需要手动创建子线程
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                //请求失败时回调
                Log.e("OkHttp","onFailure");
                e.printStackTrace();
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //请求成功时回调
                String reslut = response.body().string();
                System.out.println(reslut);
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            ArrayList<Audio> Audios = new ArrayList<>();
                            List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
                            JSONArray array = new JSONArray(reslut);
                            System.out.println( "111111111" + reslut);
                            for (int n=0;n<array.length();n++){
                                Audio audio1 = new Audio();
                                Map<String,Object> map = new HashMap<String,Object>();
                                JSONObject object1 = (JSONObject) array.get(n);
                                audio1.setImage(object1.getString("audioPic"));
                                map.put("Img",object1.getString("audioPic"));
                                audio1.setText1(object1.getString("audioName"));
                                map.put("Name",(object1.getString("audioName")));
                                audio1.setText2(object1.getString("audioAuthor"));
                                map.put("Author",object1.getString("audioAuthor"));
                                audio1.setText3(object1.getString("type"));
                                map.put("Times",object1.getString("type"));
                                audio1.setUrl(object1.getString("audioUrl"));
                                map.put("Url",object1.getString("audioUrl"));
                                audio1.setId(object1.getString("audioId"));
                                map.put("Id",object1.getString("audioId"));
                                Audios.add(audio1);
                                list.add(map);
                            }
                            list233.clear();
                            list233.addAll(list);
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                        LullabyListAdapter lullabyListAdapter = new LullabyListAdapter(getActivity(),list233);
                        spShow.setAdapter(lullabyListAdapter);
                    }
                });
            }
        });
    }
}